package com.yuq.sunrise.filter;

import org.apache.catalina.filters.RemoteIpFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author ：yuq
 * @date ：Created in 2019/6/1 22:10
 * @description：自定义Filter
 * @modified By：
 * @version: 1.0$
 */
public class WebConfiguration {

    @Bean
    public RemoteIpFilter remoteIpFilter() {
        return new RemoteIpFilter();
    }

    @Bean
    public FilterRegistrationBean testFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new urlFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("paramName", "paramValue");
        registration.setName("MyFilter");
        registration.setOrder(1);
        return registration;
    }

    public class urlFilter implements Filter {
        /**
         * create by: yuq
         * description: 销毁
         * create time: 2019-06-01 22:13
         *
         * @param
         * @return
         */
        @Override
        public void destroy() {
            // TODO Auto-generated method stub
        }

        /**
         * create by: yuq
         * description: 过滤所有请求
         * create time: 2019-06-01 22:13
         *
         * @param
         * @return 
         */
        @Override
        public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain)
                throws IOException, ServletException {
            // TODO Auto-generated method stub
            HttpServletRequest request = (HttpServletRequest) srequest;
            System.out.println("this is MyFilter,url :"+request.getRequestURI());
            filterChain.doFilter(srequest, sresponse);
        }

        /**
         * create by: yuq
         * description: 初始化
         * create time: 2019-06-01 22:13
         *
         * @param
         * @return
         */
        @Override
        public void init(FilterConfig arg0) throws ServletException {
            // TODO Auto-generated method stub
        }
    }
}
